#include<iostream>
#include<string.h>
using namespace std;

const int SIZE = 100;
const int TSIZE = 10;
int main(int argc, char const *argv[])
{
    // freopen("test.txt","r",stdin);
    int M; cin>>M;
    char early_id[SIZE]={0}, late_id[SIZE]={0};
    char early_time[TSIZE]="99:99:99";
    char late_time[TSIZE]="00:00:00";
    for(int _=0;_<M;++_)
    {
        char id_tmp[SIZE]; scanf("%s",id_tmp);
        char time_tmp[SIZE];
        scanf("%s",time_tmp);
        if(strcmp(time_tmp,early_time)<0) 
        {
            strcpy(early_time,time_tmp);
            strcpy(early_id,id_tmp);
        }
        scanf("%s",time_tmp);
        if(strcmp(time_tmp,late_time)>0) 
        {
            strcpy(late_time,time_tmp);
            strcpy(late_id,id_tmp);
        }
    }
    printf("%s %s",early_id,late_id);
    return 0;
}